package com.new2i.service.impl;
   
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
  
import com.new2i.dao.able.ScreenSchoolDao;
import com.new2i.model.DoctorEntity; 
import com.new2i.model.ScreenSchoolEntity;
import com.new2i.service.able.ScreenSchoolService; 

/**
 * 筛查学校的业务逻辑处理实现类
 * 
 * @since 2015/09/22
 * @author 2i
 *
 */
@Service("ScreenSchoolService")
public class ScreenSchoolServiceImpl implements ScreenSchoolService {
 
	private static Logger logger = Logger.getLogger(ScreenSchoolServiceImpl.class);
 
	@Autowired
	private ScreenSchoolDao mScreenSchoolDao; 
 
	@Override
	public List<HashMap<String, Object>> selectScreenSchoolList(DoctorEntity doctor) {
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		Integer doctor_id = doctor.getDoctor_id();
		Integer page = doctor.getPage();
		if(page == null)
			page = 0;
		if(doctor_id == null || doctor_id == 0) { 
			return list;
		} 
		try { 
			List<HashMap<String, Object>> scDoctorList = mScreenSchoolDao.selectScreenSchoolList(doctor_id, page);
			return scDoctorList;
		}catch (Exception e){
			logger.error(e.getLocalizedMessage()); 
		} 
		return list;
	}

	@Override
	public HashMap<String, Object> insertScreenSchool(ScreenSchoolEntity scSchool) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>();
		String schoolName = scSchool.getSchool_name();  
		if(schoolName == null || schoolName.length() <= 0) {
			rHashMap.put("code", 312);
			rHashMap.put("err_msg", "未填写学校名"); 
			return rHashMap;
		}
		try { 
			ScreenSchoolEntity rScSchool = mScreenSchoolDao.insertScreenSchool(scSchool);
			rHashMap.put("sc_doctor_id", rScSchool.getSc_school_id());  
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "创建筛查学校失败"); 
		} 
		return rHashMap; 
	}

	@Override
	public HashMap<String, Object> getScreenSchoolById(ScreenSchoolEntity scSchool) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>(); 
		Integer scDoctorId = scSchool.getSc_school_id();  
		if(scDoctorId == null || scDoctorId == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定筛查学校"); 
			return rHashMap;
		}  
		try {  
			HashMap<String, Object> screenSchoolInfo = mScreenSchoolDao.getScreenSchoolById(scDoctorId); 
			return screenSchoolInfo; 
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 324);
			rHashMap.put("err_msg", "获取用户出错");
		} 
		return rHashMap;  
	}

	@Override
	public HashMap<String, Object> updateScreenSchool(ScreenSchoolEntity scSchool) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>(); 
		Integer scDoctorId = scSchool.getSc_school_id();  
		String schoolName = scSchool.getSchool_name();   
		if(scDoctorId == null || scDoctorId == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定筛查学校"); 
			return rHashMap;
		}
		if(schoolName == null || schoolName.length() <= 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "请输入筛查学校名"); 
			return rHashMap;
		}  
		try {  
			Integer updateRows = mScreenSchoolDao.updateScreenSchool(scSchool); 
			if(updateRows > 0) { 
				rHashMap.put("sc_doctor_id", scDoctorId); 
				rHashMap.put("msg", "修改筛查学校信息成功");  
			}else {
				rHashMap.put("code", 322);
				rHashMap.put("err_msg", "修改筛查学校信息失败"); 
			}   
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "修改筛查学校信息出错");
		} 
		return rHashMap;  
	}

	@Override
	public HashMap<String, Object> removeScreenSchool(ScreenSchoolEntity scSchool) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>(); 
		Integer sc_school_id = scSchool.getSc_school_id();
		Integer doctor_id = scSchool.getDoctor_id(); 
		if(doctor_id == null || doctor_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定当前机构操作医生"); 
			return rHashMap;
		}  
		if(sc_school_id == null || sc_school_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定筛查学校"); 
			return rHashMap;
		}   
		try {   
			Integer updateRows = mScreenSchoolDao.removeScreenSchool(sc_school_id, doctor_id);
			if(updateRows > 0) { 
				rHashMap.put("sc_doctor_id", sc_school_id); 
				rHashMap.put("msg", "删除筛查学校成功");  
			}else {
				rHashMap.put("code", 322);
				rHashMap.put("err_msg", "筛查学校已被删除"); 
			}  
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "删除筛查学校出错");
		} 
		return rHashMap;
	}

	@Override
	public HashMap<String, Object> backupScreenSchool(ScreenSchoolEntity scSchool) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>(); 
		Integer sc_school_id = scSchool.getSc_school_id();
		Integer doctor_id = scSchool.getDoctor_id(); 
		if(doctor_id == null || doctor_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定当前机构操作医生"); 
			return rHashMap;
		}  
		if(sc_school_id == null || sc_school_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定筛查学校"); 
			return rHashMap;
		}   
		try {   
			Integer updateRows = mScreenSchoolDao.backupScreenSchool(sc_school_id, doctor_id);
			if(updateRows > 0) { 
				rHashMap.put("sc_doctor_id", sc_school_id); 
				rHashMap.put("msg", "还原筛查学校成功");  
			}else {
				rHashMap.put("code", 322);
				rHashMap.put("err_msg", "还原学校已被删除"); 
			}  
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "还原筛查学校出错");
		} 
		return rHashMap;
	}

	@Override
	public HashMap<String, Object> deleteScreenSchool(ScreenSchoolEntity scSchool) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>(); 
		Integer sc_school_id = scSchool.getSc_school_id();
		Integer doctor_id = scSchool.getDoctor_id(); 
		if(doctor_id == null || doctor_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定当前机构操作医生"); 
			return rHashMap;
		}  
		if(sc_school_id == null || sc_school_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定筛查学校"); 
			return rHashMap;
		}   
		try {   
			Integer updateRows = mScreenSchoolDao.deleteScreenSchool(sc_school_id, doctor_id);
			if(updateRows > 0) { 
				rHashMap.put("sc_doctor_id", sc_school_id); 
				rHashMap.put("msg", "删除筛查学校成功");  
			}else {
				rHashMap.put("code", 322);
				rHashMap.put("err_msg", "筛查学校已被删除"); 
			}  
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "删除筛查学校出错");
		} 
		return rHashMap;
	}     
}
